/*
 * @Descripttion:
 * @version: 1.0
 * @Author: wuhao
 * @Date: 2021-10-08 15:02:53
 * @LastEditors: wuhao
 * @LastEditTime: 2021-10-11 10:45:35
 */
const curEnv = process.env.NODE_ENV
let curEnvParam = require('../env/' + curEnv)
/**
 * 功能: 数据加*
 * 参数: str：字符串，frontLen：前面保留位数，endLen：后面保留位数
 * 版本: 1.0
 */
export function plusXing(str, frontLen, endLen) {
  if (!str || str.length < (frontLen + endLen)) {
    return str;
  }
  var len = str.length - frontLen - endLen;
  var xing = '';
  for (var i = 0; i < len; i++) {
    xing += '*';
  }
  return str.substring(0, frontLen) + xing + str.substring(str.length - endLen);
}

export function encodeUsername(username) {
  return plusXing(username, 1, 0)
}

export function encodeIdCard(idCard) {
  return plusXing(idCard, 3, 3)
}

export function alertInfo(info) {
  if (curEnvParam.env == 'dev') {
    alert(info)
  }
}

export function toNOData(info, close) {
  let closeCurrentPage = 1;
  if (close != null) {
    closeCurrentPage = close;
  }
  this.timer = setTimeout(() => { //设置延迟执行
    ehbAppJssdk.notice.hidePreloader();
    ehbAppJssdk.operateWindow.open({
      title: '',
      url: absoultPath + "/noData",
      urlType: 'webPage',
      backRefresh: '1',
      reloadForLogin: '1',
      closeCurrentPage: closeCurrentPage,
      disableScrolling: 'disable',
      pageType: '1',
      pageLevel: '2',
      params: {
        info: info
      }
    });
  }, 1000);
};

//对于一些空值进行默认处理,type：面积有单位㎡，特殊处理。
export function defaultValue(value, type) {
  //空字符串、null
  if (value == null || "" === value) {
    return "暂无";
  } else {
    //面积
    if (type === 1) {
      return value + "㎡";
    } else if (type === 2) {
      //电话或其他
      return value;
    }
  }
};

export function copyDeep(templateData) {
  // templateData 是要复制的数组或对象，这样的数组或者对象就是指向新的地址的
  return JSON.parse(JSON.stringify(templateData));
}

/**
 * 判断字符串是否空或空格
 * @param str
 * @returns {boolean}
 */
export function isEmptyStr(str) {
  if (!str) {
    return true;
  }
  var regu = "^[ ]+$";
  var re = new RegExp(regu);
  return re.test(str);
}

/**
 * 生成UUID
 * @returns {string}
 */
export function uuid() {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = "-";

  var uuid = s.join("");
  return uuid;
}

/**
 *判断身份证号是否符合规则
 * @param code
 * @returns {boolean}
 */
export function IdentityCodeValid(code) {
  var city = {
    11: "北京",
    12: "天津",
    13: "河北",
    14: "山西",
    15: "内蒙古",
    21: "辽宁",
    22: "吉林",
    23: "黑龙江 ",
    31: "上海",
    32: "江苏",
    33: "浙江",
    34: "安徽",
    35: "福建",
    36: "江西",
    37: "山东",
    41: "河南",
    42: "湖北 ",
    43: "湖南",
    44: "广东",
    45: "广西",
    46: "海南",
    50: "重庆",
    51: "四川",
    52: "贵州",
    53: "云南",
    54: "西藏 ",
    61: "陕西",
    62: "甘肃",
    63: "青海",
    64: "宁夏",
    65: "新疆",
    71: "台湾",
    81: "香港",
    82: "澳门",
    91: "国外 "
  };
  var tip = "";
  var pass = true;

  if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)) {
    tip = "身份证号格式错误";
    pass = false;
  } else if (!city[code.substr(0, 2)]) {
    tip = "地址编码错误";
    pass = false;
  } else {
    //18位身份证需要验证最后一位校验位
    if (code.length == 18) {
      code = code.split('');
      //∑(ai×Wi)(mod 11)
      //加权因子
      var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
      //校验位
      var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
      var sum = 0;
      var ai = 0;
      var wi = 0;
      for (var i = 0; i < 17; i++) {
        ai = code[i];
        wi = factor[i];
        sum += ai * wi;
      }
      var last = parity[sum % 11];
      if (parity[sum % 11] != code[17]) {
        tip = "校验位错误";
        pass = false;
      }
    }
  }
  return pass;
}

/**
 * 保存操作日志
 * operateType 操作类型
 * operateInfo 操作信息
 */
export function saveLog(operateType, operateInfo, gnid) {
  let that = this;
  that.sysLog.operatetype = operateType;
  if (operateInfo != null) {
    that.sysLog.operateinfo = operateInfo;
  }
  that.sysLog.gnid = gnid;
  ehbAppJssdk.network.request({
    url: that.GLOBAL.urlConfig.saveSysLogInfo[that.GLOBAL.urlType],
    method: 'post',
    data: {
      'log': JSON.stringify(that.sysLog)
    },
    headers: {
      'contentType': 'application/x-www-form-urlencoded'
    },
    success: function (res) {}
  });
}

/**
 * 手机号码长度验证
 * @returns {boolean}
 */
export function isMobile(mobile) {
  var pass = true;
  if (!mobile || !/^1[1|2|3|4|5|6|7|8|9][0-9]{9}$/i.test(mobile)) {
    pass = false;
  }

  return pass;;
}
